# ======================================================================
#
# Copyright (C) ADN
# ADN::SERVER::ParsePOST
# Description:
#  - Lance les processus des requete POST
#  - Launch processus from POST request
#
# ======================================================================

package ADN::SERVER::ParsePOST;

use strict;
use ADN::LOG::Log;
use ADN::CONF::Path;
use ADN::LANG::Lang;
use ADN::CONF::Read;
use Proc::Background;
use ADN::XML::XMLtoOBJ;
use ADN::SERVER::Constant qw(:constant :log :param);
use ADN::SERVICE::Service;

# #######################
# LOG & LANG
# #######################
sub new {
	  my $self = shift;
	  my $class = ref($self) || $self;
	  return $self if ref $self;
	  $self = bless {} => $class;
    $self->{log} = ADN::LOG::Log->new();
    %{$self->{lang}} = ADN::LANG::Lang->new()->ParsePOST();
    return $self;
}

# #######################
# PARSE THE ELEMENT
# #######################
sub start
{
    my $self = shift;
    my ( $pid, $path );
    %{$self->{param}} = @_;
    my $gid = $self->{param}{GID};    
    %{$self->{service}} = ADN::SERVICE::Service->new()->$gid($self->{param}{Name});
    $self->{log}->Log( 'TYPE : ' . $self->{service}{type}, HIGH, ALL );
    if ( $self->{service}{type} eq C_FALSE ) {
	   $self->{log}->Log( $self->{lang}{mess1}, HIGH, ALL );
	   return C_FALSE;
    }
    elsif ( $self->{service}{type} == -2 || $self->{service}{type} eq '' || $self->{service}{type} == -3 ) {
	   $self->{log}->Log( $self->{lang}{mess2}, HIGH, ALL );
	   return C_FALSE;
    }
    elsif ( $self->{service}{type} eq 'ADN' ) {
	   return 1;
    }
    if ( $self->{service}{type} eq 'simple' || $self->{service}{type} eq 'complex' ) {
	   $pid = $self->launcher( $self->{service}{param}, $self->{service}{function} );
    }
    else {
	   $self->{log}->Log( $self->{lang}{mess2}, HIGH, ALL );
	   return C_FALSE;
    }
    $path = ADN::SERVER::StartUP->temp();
    $path .= 'process';
    if (open WRITER, '>>', $path) {
      print WRITER $self->{service}{function} . '/' . $pid . "\n";
      if (!close WRITER) {}
    }
    undef $path;
		undef $gid;
    return $self->{service}{function} . '/' .$pid;
}

# #######################
# PUT THE PARAM
# #######################
sub param {
    my $self = shift;
    my ( $param, $tmp);
    $param = ADN::XML::XMLtoOBJ->new( $self->{param}{XML} );
    $tmp = $self->{service}{'p' . $_[0]};
    $param->$tmp;
    undef $tmp;
    return $param->_server_arg_(0);
}

# #######################
# LAUNCH THE SCRIPT
# #######################
sub launcher {
    my $self = shift;
    ( $self->{nbr}, $self->{func} ) = @_;
		$self->_initscript();
    $self->{log}->Log( 'LAUNCHER : ' . $self->{func} . ' ' . @{$self->{inet}}[0] . ' ' . $self->{uri} . ' ' . $self->{arg} , HIGH, ALL );
    $self->{log}->Log( 'ARGUMENT : ' . $self->{arg} , HIGH, ALL );
    $self->{kid} = Proc::Background->new( $self->{serviceBin}, $self->{func}, @{$self->{inet}}[0], $self->{uri}, $self->{arg} );
    $self->{kid} = $self->{kid}->pid;
    $self->{func} = $self->{path} . $self->{folder}{service_data} . 'Clean.pl';
    Proc::Background->new( $self->{serviceBin}, $self->{func} );
    if ( $self->{kid} == 0 ) {
	   $self->{log}->Log( $self->{lang}{mess3}, HIGH, ALL );
	   return;
    }
    return  @{$self->{id}}[0] . '/' . $self->{kid};
}

# #######################
# INITIALIZATION LAUNCH THE SCRIPT
# #######################
sub _initscript {
    my $self = shift;
    my ( $conf, $i, $service);

    $conf = ADN::CONF::Read->new();
    $self->{path} = $conf->_send_path();
    %{$self->{folder}} = $conf->service_server();
    undef $conf;
		$service = ADN::XML::XMLtoOBJ->new( $self->{param}{XML} );
    @{$self->{urn}} = $service->urn();
    @{$self->{id}} = $service->IdClient();
    @{$self->{inet}} = $service->IpServices();
    undef $service;
    $self->{func} = $self->{path} . $self->{folder}{service_folder} . $self->{func};   
		$self->{serviceBin} = ADN::CONF::Path->new( $self->{folder}{bin} );
    $self->{uri} = @{$self->{id}}[0] . '/' . @{$self->{urn}}[0];
    $self->{arg} = '';
    for ( $i = 1; $i <= $self->{nbr}; $i++ ) {
	   $self->{arg} .= $self->param( $i ) . BIGPARAM;
    }
    undef $i;
    return;
}

# #######################
# DESTROY OBJECT
# #######################
sub DESTROY {
    my $self = shift;
    return;
}
1;
__END__
